package com.xindaoapp.happypet.fragments;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.MyPostListAdapter2;
import com.xindaoapp.happypet.adapter.MyPostListAdapter2.OnNextPageListener;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.ui.BaseFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 我的收藏
 * 
 * @author xindaoapp
 * 
 */
public class MyCollectFragment extends BaseFragment {
    public static Fragment newInstance(boolean isBack) {
        Fragment fragment = new MyCollectFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(Constants.KEY_IS_CLICK_TO_BACK, isBack);
        fragment.setArguments(bundle);
        return fragment;
    }

    private PullToRefreshListView vPullToRefreshListView;
    private MyPostListAdapter2 mAdapter2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_listview, container, false);
        vPullToRefreshListView = (PullToRefreshListView) rootView.findViewById(R.id.listview);
        vPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        vPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                loadDatas();
            }
        });
        vPullToRefreshListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (getArguments().getBoolean(Constants.KEY_IS_CLICK_TO_BACK)) {
                    Message info = (Message) parent.getAdapter().getItem(position);
                    Intent data = new Intent();
                    data.putExtra(Constants.KEY_POST_NAME, info.subject);
                    data.putExtra(Constants.KEY_POST_TID, info.tid);
                    if (info.pics.size() > 0) {
                        data.putExtra(Constants.KEY_POST_COVER, info.pics.get(0).url);
                    }
                    getActivity().setResult(Activity.RESULT_OK, data);
                    getActivity().finish();
                } else {// 跳转详情

                }
            }
        });
    }

    @Override
    protected void loadDatas() {
        super.loadDatas();
        HappyPetApplication
                .get()
                .getMoccaApi()
                .getMyCollect(UserState.getUserUid(), Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                        new IRequest<List<Message>>() {

                            @Override
                            public void request(List<Message> t) {
                                if (null != t) {
                                    onDataArrived(true);
                                    if (t.size() == 0) {
                                        onDataArrivedEmpty();
                                    } else {
                                        vPullToRefreshListView.setAdapter(mAdapter2 =
                                                new MyPostListAdapter2(getActivity(), t,
                                                        Constants.PAGE_SIZE,
                                                        R.layout.activity_my_club_item,
                                                        R.layout.item_loading));
                                        mAdapter2.setOnNextPageListener(mOnNextPageListener);
                                    }
                                } else {
                                    onDataArrived(false);
                                }
                                vPullToRefreshListView.onRefreshComplete();
                            }
                        });
    }

    OnNextPageListener mOnNextPageListener = new OnNextPageListener() {

        @Override
        public void nextPage(int start, int size, final ILoadNextPageData<Message> iLoadNextPageData) {
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getMyCollect(UserState.getUserUid(), start, size,
                            new IRequest<List<Message>>() {

                                @Override
                                public void request(List<Message> t) {
                                    iLoadNextPageData.loadNextPageData(t);
                                }
                            });
        }
    };
}
